package com.briup.es.service.impl;

import com.briup.es.config.auth.UserDetails;
import com.briup.es.config.response.ResultCode;
import com.briup.es.domain.vo.CollectVO;
import com.briup.es.mapper.BookMapper;
import com.briup.es.mapper.CollectMapper;
import com.briup.es.service.CollectService;
import com.briup.es.utils.AssertUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private BookMapper bookMapper;

    @Override
    public List<CollectVO> queryAll() {
        Integer userId = UserDetails.userId();
        return collectMapper.queryAllByUserId(userId);
    }

    @Override
    public void cancelCollectBatch(List<Integer> ids) {
        Integer userId = UserDetails.userId();
        collectMapper.cancelCollectBatch(ids,userId);
    }

    @Override
    public void addCollect(Integer bookId) {
        // 根据图书id查询指定图书的数量
        int count = bookMapper.queryById(bookId);
        AssertUtil.isTrue(count == 1,
                ResultCode.BOOK_IS_NOT_EXISTS);

        // 图书存在，添加收藏
        collectMapper.addCollect(UserDetails.userId(),bookId);
    }
}
